package com.dyh.quickdemozwei.shiyanchang.pipeLine;



import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;

public class Blueprint {


    public static void main(String[] args) {
//        test1();
        test3();
    }

    static final Function<String,Integer> str2int = Integer::valueOf;
    static final Function<String,Long> str2long = Long::valueOf;
    static final Function<String,Double> str2ldouble = Double::valueOf;
    static final Function<String,Float> str2float = Float::valueOf;
    public static Map<String,Function<String,?>> commonFunctions = new HashMap<>();
    static {
        commonFunctions.put("str2int",str2int);
        commonFunctions.put("str2long",str2long);
        commonFunctions.put("str2ldouble",str2ldouble);
        commonFunctions.put("str2float",str2float);
    }


    public  static void test3(){
        String str = "111|str2long|f1";




        Function<Long,Long> f1 = e->e+1;
        Map<String,Function> functionMap = new HashMap<>();

        functionMap.put("f1",f1);

        String[] arr = str.split("\\|");
        String obj = arr[0];


        Function<String,Function> functionMapper = e->{
            Function r =  commonFunctions.get(e);
            if (r!=null)
                return r;
            else
              return   functionMap.get(e);
        };

        List<Function> functions = new ArrayList<>();
        for (int i = 1 ;i<arr.length;i++){
            functions.add(functionMapper.apply(arr[i]));
        }




        Object result =  prototype(obj,functions);

        System.out.println(result);

    }


    public  static void test1(){
       String str = "xxx|f1|f2";



        Function<String,String> f1 = e->e+"!";
        Function<String,Integer> f2 = String::length;
        Map<String,Function> functionMap = new HashMap<>();
        functionMap.put("f1",f1);
        functionMap.put("f2",f2);

        String[] arr = str.split("\\|");
       String obj = arr[0];



        List<Function> functions = new ArrayList<>();
        for (int i = 1 ;i<arr.length;i++){
            functions.add(functionMap.get(arr[i]));
        }




        Object result =  prototype(obj,functions);

        System.out.println();

    }


    public void test(){
        Object obj = "xxx";

        Supplier<Object>  sp = ()->"xx";
        Function<String,String> f1 = e->e+"!";
        Function<String,Integer> f2 = String::length;

        Object result1 =  prototype(obj,f1,f2);
        Object result2 =  prototype(sp,f1,f2);

        System.out.println(obj);
        System.out.println(result1);
        System.out.println(result2);

    }


    static Object  prototype(Object obj, List<Function> functions){
        for (Function function : functions) {
            obj = function.apply(obj);
        }
        return obj;
    }


   static Object  prototype( Object obj,Function... functions){
       for (Function function : functions) {
           obj = function.apply(obj);
       }
       return obj;
   }

    static Object  prototype( Supplier supplier,Function... functions){
        Object obj = supplier.get();
        for (Function function : functions) {
            obj = function.apply(obj);
        }
        return obj;
    }
}
